/**
 * @author Administrador
 * Created on 17/11/2005
 * Updated on 02/04/2010
 */
package br.unb.graphics.qos.charts;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.LineNumberReader;
import java.util.StringTokenizer;
import java.util.Vector;

import br.unb.core.traffic.Traffic;

public class JitterChart extends GenericQosChart {

    private double delay;
    private static final String JITTER = "jitter";//will look in on QosChart.properties

    public JitterChart() {
        super(JITTER);
    }

    public void addQosToTraffic(
            int nrPacket,
            int lenPacket,
            long sendTime,
            long receiveTime,
            String trafficName) {
        Vector values = (Vector) super.vectorsMap.get(trafficName);
        if (values != null) {
            double jitter, lastDelay = this.delay;
            this.delay = (receiveTime - sendTime) / 1000.0d;
            jitter = Math.abs(this.delay - lastDelay);
            if (nrPacket > 1) {
                values.add(new Value(receiveTime, jitter));
            }
        }
    }

    @Override
    public void loadValuesFromFile(File file) {
        //This method only works suposing the graffics where added on this order:
        //delay, jitter, loss, bandwidth

        try {
            FileInputStream fis = new FileInputStream(file);
            LineNumberReader lnr = new LineNumberReader(new InputStreamReader(fis));
            String nextStringTraffic = "";
            StringTokenizer st;

            int start = 0;
            String trafficName;
            while (!nextStringTraffic.startsWith("#Traffic:") && ((nextStringTraffic = lnr.readLine()) != null));
            //		for(int i = 0; i<4 && ((nextStringTraffic = lnr.readLine())!=null );i++);
            if (nextStringTraffic.startsWith("#Traffic:")) {

                Traffic traffic = Traffic.fromString(nextStringTraffic.substring(nextStringTraffic.indexOf(":") + 1));
                trafficName = traffic.getName();
                start = traffic.getStart();
            } else {
                trafficName = file.getName();
            }

            this.addTraffic(trafficName);
            double delay = 0, jitter = 0;
            long receiveTime = 0, sendTime, refReceiveTime = 0;
            double lastDelay = 0;
            while ((nextStringTraffic = lnr.readLine()) != null
                    && !nextStringTraffic.startsWith("#NrPacket"));
            if ((nextStringTraffic = lnr.readLine()) != null) {
                st = new StringTokenizer(nextStringTraffic, "\t");
                try {
                    st.nextToken();
                    st.nextToken();
                    sendTime = Long.parseLong(st.nextToken());
                    receiveTime = Long.parseLong(st.nextToken());
                    delay = (receiveTime - sendTime) / 1000.0d;
                    refReceiveTime = receiveTime - 3 * 60 * 60 * 1000000L - 1000000L * start;

                } catch (NumberFormatException nfe) {
                }
            }
            while ((nextStringTraffic = lnr.readLine()) != null) {
                st = new StringTokenizer(nextStringTraffic, "\t");
                try {

                    st.nextToken();
                    st.nextToken();
                    sendTime = Long.parseLong(st.nextToken());
                    receiveTime = Long.parseLong(st.nextToken());

                    //Delay e Jitter
                    lastDelay = delay;
                    delay = (receiveTime - sendTime) / 1000.0d;
                    jitter = Math.abs(delay - lastDelay);
                    this.addValueToTraffic(jitter, (receiveTime - refReceiveTime), trafficName);

                } catch (NumberFormatException nfe) {
                }
            }
            lnr.close();
            fis.close();

        } catch (IOException ioe) {
        }
        Runtime.getRuntime().gc();
    }
}
